package com.win.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.win.feign.ProductFeignClient;
import com.win.pojo.Order;
import com.win.pojo.Product;
import com.win.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    DiscoveryClient discoveryClient;
    @Autowired
    RestTemplate restTemplate;

    @Autowired
    LoadBalancerClient loadBalancerClient;

    @Autowired
    ProductFeignClient productFeignClient;

    @SentinelResource(value = "createOrderImpl")
    @Override
    public Order createOrder(Long productId, Long userId) {
//        Product productRemote = getProductRemote(productId);
//        Product productRemote = getProductLoadBalancerClientRemoteAnnotation(productId);
        Product productRemote = productFeignClient.product(productId);
        Order order = new Order();
        order.setId(productId);
        order.setUserId(userId);
        order.setNickName("Win");

        order.setAddress("洛阳");
        order.setTotalAmount(productRemote.getPrice().multiply(BigDecimal.valueOf(productRemote.getNum())));
        order.setProductList(Arrays.asList(productRemote));
        return order;
    }

    public Product getProductRemote(Long productId){
        // 获取商品服务器的ip+port
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        //先问nacos 第一个在线的容器地址
        ServiceInstance serviceInstance = instances.get(0);
        String url = "http://"+serviceInstance.getHost()+":"+ serviceInstance.getPort()+"/product/"+productId;
        log.info("远程url：{}",url);
        //给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    public Product getProductLoadBalancerClientRemote(Long productId){
        // 获取商品服务器的ip+port
        ServiceInstance choose = loadBalancerClient.choose("service-product");
        String url = "http://"+choose.getHost()+":"+ choose.getPort()+"/product/"+productId;
        log.info("远程url：{}",url);
        //给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    public Product getProductLoadBalancerClientRemoteAnnotation(Long productId){
        // 获取商品服务器的ip+port
        ServiceInstance choose = loadBalancerClient.choose("service-product");
//        String url = "http://"+choose.getHost()+":"+ choose.getPort()+"/product/"+productId;
        String url = "http://service-product/product"+productId;
        log.info("远程url：{}",url);
        //给远程发送请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

}
